Data visualization
- M1 MIDS & MFA
- Université Paris Cité
- Année 2024-2025
-
Course Homepage
- Moodle
This workbook introduces visualization according to the Grammar of Graphics framework.
Using ggplot2, we reproduce Rosling’s gapminder talk.
This is an opportunity to develop the layered construction of graphical objects.
Grammar of Graphics
We will use the Grammar of Graphics approach to visualization
The expression Grammar of Graphics was coined by Leiland Wilkinson to describe a principled approach to visualization in Data Analysis (EDA)
A plot is organized around tabular data (a table with rows (observations) and columns (variables))
A plot is a graphical object that can be built layer by layer
Building a graphical object consists in chaining elementary operations
The acclaimed TED presentation by Hans Rosling illustrates the Grammar of Graphics approach
We will reproduce the animated demonstration using
Setup
We will use the following packages. If needed, we install them.
The data we will use can be obtained by loading package gapminder
If the packages have not yet been installed on your hard drive, install them.
You can do that using base R install.packages() function:
install.packages("tidyverse")It is often faster to use functions from package pak
install.packages("pak")
pak::pkg_install("tidyverse")You need to understand the difference between installing and loading a package
- How do we get the list of installed packages?
- How do we get the list of loaded packages?
- Which objects are made available by a package?
The (usually very long) list of installed packages can be obtained by a simple function call.
Code
df <- installed.packages()
head(df)
## Package LibPath
## abind "abind" "/home/boucheron/R/x86_64-pc-linux-gnu-library/4.4"
## arkhe "arkhe" "/home/boucheron/R/x86_64-pc-linux-gnu-library/4.4"
## arrow "arrow" "/home/boucheron/R/x86_64-pc-linux-gnu-library/4.4"
## ash "ash" "/home/boucheron/R/x86_64-pc-linux-gnu-library/4.4"
## AsioHeaders "AsioHeaders" "/home/boucheron/R/x86_64-pc-linux-gnu-library/4.4"
## askpass "askpass" "/home/boucheron/R/x86_64-pc-linux-gnu-library/4.4"
## Version Priority Depends
## abind "1.4-5" NA "R (>= 1.5.0)"
## arkhe "1.6.0" NA "R (>= 3.5)"
## arrow "16.1.0" NA "R (>= 4.0)"
## ash "1.0-15" NA NA
## AsioHeaders "1.22.1-2" NA NA
## askpass "1.2.0" NA NA
## Imports
## abind "methods, utils"
## arkhe "graphics, methods, stats, utils"
## arrow "assertthat, bit64 (>= 0.9-7), glue, methods, purrr, R6, rlang\n(>= 1.0.0), stats, tidyselect (>= 1.0.0), utils, vctrs"
## ash NA
## AsioHeaders NA
## askpass "sys (>= 2.1)"
## LinkingTo
## abind NA
## arkhe NA
## arrow "cpp11 (>= 0.4.2)"
## ash NA
## AsioHeaders NA
## askpass NA
## Suggests
## abind NA
## arkhe "tinytest"
## arrow "blob, curl, cli, DBI, dbplyr, decor, distro, dplyr, duckdb\n(>= 0.2.8), hms, jsonlite, knitr, lubridate, pillar, pkgload,\nreticulate, rmarkdown, stringi, stringr, sys, testthat (>=\n3.1.0), tibble, tzdb, withr"
## ash NA
## AsioHeaders NA
## askpass "testthat"
## Enhances License License_is_FOSS
## abind NA "LGPL (>= 2)" NA
## arkhe NA "GPL (>= 3)" NA
## arrow NA "Apache License (>= 2.0)" NA
## ash NA "GPL (>= 2)" NA
## AsioHeaders NA "BSL-1.0" NA
## askpass NA "MIT + file LICENSE" NA
## License_restricts_use OS_type MD5sum NeedsCompilation Built
## abind NA NA NA "no" "4.4.0"
## arkhe NA NA NA "no" "4.4.0"
## arrow NA NA NA "yes" "4.4.0"
## ash NA NA NA "yes" "4.4.0"
## AsioHeaders NA NA NA "no" "4.4.0"
## askpass NA NA NA "yes" "4.4.0"Note that the output is tabular (it is a matrix and an array) that contains much more than the names of installed packages. If we just want the names of the installed packages, we can extract the column named Package.
Code
df[1:5, c("Package", "Version") ]
## Package Version
## abind "abind" "1.4-5"
## arkhe "arkhe" "1.6.0"
## arrow "arrow" "16.1.0"
## ash "ash" "1.0-15"
## AsioHeaders "AsioHeaders" "1.22.1-2"Matrices and arrays represent mathematical object and are fit for computations. They are not so convenient as far as querying is concerned. Dataframes which are also tabular objects can be queried like tables in a relational database.
Loading a package amounts to make a number of objects available in the current session. The objects are made available though Namespaces.
Code
loadedNamespaces()
## [1] "methods" "graphics" "plotly" "utf8" "generics"
## [6] "tidyr" "stringi" "hms" "digest" "magrittr"
## [11] "evaluate" "grid" "timechange" "grDevices" "fastmap"
## [16] "jsonlite" "ggrepel" "tidyverse" "ggthemes" "httr"
## [21] "purrr" "fansi" "viridisLite" "scales" "tweenr"
## [26] "codetools" "lazyeval" "cli" "rlang" "polyclip"
## [31] "munsell" "withr" "utils" "yaml" "stats"
## [36] "tools" "base" "tzdb" "dplyr" "colorspace"
## [41] "ggplot2" "forcats" "vctrs" "R6" "lifecycle"
## [46] "lubridate" "stringr" "htmlwidgets" "MASS" "pkgconfig"
## [51] "pillar" "gtable" "glue" "data.table" "Rcpp"
## [56] "ggforce" "xfun" "tibble" "tidyselect" "knitr"
## [61] "farver" "datasets" "gapminder" "htmltools" "patchwork"
## [66] "rmarkdown" "readr" "compiler"Note that we did not load explicitly some of the loadedNamespaces. Many of the loaded packages were loaded while loading other packages, for example metapackages like tidyverse.
Have a look at gapminder dataset
The gapminder table can be found at gapminder::gapminder
- A table has a schema: a list of named columns, each with a given type
- A table has a content: rows. Each row is a collection of items, corresponding to the columns
Explore gapminder::gapminder, using glimpse() and head()
Dataframes
Code
gapminder <- gapminder::gapminder
glimpse(gapminder)
## Rows: 1,704
## Columns: 6
## $ country <fct> "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", …
## $ continent <fct> Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, …
## $ year <int> 1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, …
## $ lifeExp <dbl> 28.801, 30.332, 31.997, 34.020, 36.088, 38.438, 39.854, 40.8…
## $ pop <int> 8425333, 9240934, 10267083, 11537966, 13079460, 14880372, 12…
## $ gdpPercap <dbl> 779.4453, 820.8530, 853.1007, 836.1971, 739.9811, 786.1134, …
gapminder |>
glimpse()
## Rows: 1,704
## Columns: 6
## $ country <fct> "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", …
## $ continent <fct> Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, …
## $ year <int> 1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, …
## $ lifeExp <dbl> 28.801, 30.332, 31.997, 34.020, 36.088, 38.438, 39.854, 40.8…
## $ pop <int> 8425333, 9240934, 10267083, 11537966, 13079460, 14880372, 12…
## $ gdpPercap <dbl> 779.4453, 820.8530, 853.1007, 836.1971, 739.9811, 786.1134, …
gapminder |>
head()
## # A tibble: 6 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Afghanistan Asia 1952 28.8 8425333 779.
## 2 Afghanistan Asia 1957 30.3 9240934 821.
## 3 Afghanistan Asia 1962 32.0 10267083 853.
## 4 Afghanistan Asia 1967 34.0 11537966 836.
## 5 Afghanistan Asia 1972 36.1 13079460 740.
## 6 Afghanistan Asia 1977 38.4 14880372 786.Even an empty dataframe has a scheme:
The schema of a dataframe/tibble is the list of column names and classes. The content of a dataframe is made of the rows. A dataframe may have null content
Get a feeling of the dataset
Pick two random rows for each continent using slice_sample()
To pick a slice at random, we can use function slice_sample. We can even perform sampling within groups defined by the value of a column.
Code
gapminder |>
slice_sample(n=2, by=continent)# A tibble: 10 × 6
country continent year lifeExp pop gdpPercap
<fct> <fct> <int> <dbl> <int> <dbl>
1 Mongolia Asia 1957 45.2 882134 913.
2 Pakistan Asia 1967 49.8 60641899 942.
3 Ireland Europe 1992 75.5 3557761 17559.
4 Netherlands Europe 1967 73.8 12596822 15363.
5 Zimbabwe Africa 1987 62.4 9216418 706.
6 Chad Africa 1967 43.6 3495967 1197.
7 Canada Americas 1962 71.3 18985849 13462.
8 Canada Americas 1967 72.1 20819767 16077.
9 New Zealand Oceania 2002 79.1 3908037 23190.
10 New Zealand Oceania 1977 72.2 3164900 16234.
Code
#< or equivalently
# gapminder |>
# group_by(continent) |>
# slice_sample(n=2)What makes a table tidy?
Have a look at Data tidying in R for Data Science (2nd ed.)
Is the gapminder table redundant?
gapminder is redundant: column country completely determines the content of column continent. In database parlance, we have a functional dependancy: country → continent whereas the key of the table is made of columns country, year.
Table gapminder is not in Boyce-Codd Normal Form (BCNF), not even in Third Normal Form (3NF).
Gapminder tibble (extract)
Extract/filter a subset of rows using dplyr::filter(...)
- All rows concerning a given country
- All rows concerning a year
- All rows concerning a given continnent and a year
# A tibble: 6 × 6
country continent year lifeExp pop gdpPercap
<fct> <fct> <int> <dbl> <int> <dbl>
1 France Europe 1952 67.4 42459667 7030.
2 France Europe 1957 68.9 44310863 8663.
3 France Europe 1962 70.5 47124000 10560.
4 France Europe 1967 71.6 49569000 13000.
5 France Europe 1972 72.4 51732000 16107.
6 France Europe 1977 73.8 53165019 18293.
Equality testing is performed using ==, not = (which is used to implement assignment)
Filtering (selection \(σ\) from database theory) : Picking one year of data
There is simple way to filter rows satisfying some condition. It consists in mimicking indexation in a matrix, leaving the colum index empty, replacing the row index by a condition statement (a logical expression) also called a mask.
Code
gapminder_2002 <- gapminder[gapminder$year==2002, ]Have a look at gapminder$year==2002. What is the type/class of this expression?
This is possible in base R and very often convenient.
Nevertheless, this way of performing row filtering does not emphasize the connection between the dataframe and the condition. Any logical vector with the right length could be used as a mask. Moreover, this way of performing filtering is not very functional.
In the parlance of Relational Algebra, filter performs a selection of rows. Relational expression \[σ_{\text{condition}}(\text{Table})\] translates to
filter(Table, condition)where \(\text{condition}\) is a boolean expression that can be evaluated on each row of \(\text{Table}\). In SQL, the relational expression would translate into
SELECT *
FROM Table
WHERE conditionCheck Package dplyr docs
The posit cheatsheet on dplyr is an unvaluable resource for table manipulation.
Use dplyr::filter() to perform row filtering
Code
# filter(gapminder, year==2002)
gapminder |>
filter(year==2002)# A tibble: 142 × 6
country continent year lifeExp pop gdpPercap
<fct> <fct> <int> <dbl> <int> <dbl>
1 Afghanistan Asia 2002 42.1 25268405 727.
2 Albania Europe 2002 75.7 3508512 4604.
3 Algeria Africa 2002 71.0 31287142 5288.
4 Angola Africa 2002 41.0 10866106 2773.
5 Argentina Americas 2002 74.3 38331121 8798.
6 Australia Oceania 2002 80.4 19546792 30688.
7 Austria Europe 2002 79.0 8148312 32418.
8 Bahrain Asia 2002 74.8 656397 23404.
9 Bangladesh Asia 2002 62.0 135656790 1136.
10 Belgium Europe 2002 78.3 10311970 30486.
# ℹ 132 more rows
Note that in stating the condition, we simply write year==2002 even though year is not the name of an object in our current session. This is possible because filter( ) uses data masking, year is meant to denote a column in gapminder.
The ability to use data masking is one of the great strengths of the R programming language.
Static plotting: First attempt
Define a plot with respect to gapminder_2002 along the lines suggested by Rosling’s presentation.
Code
p <- gapminder_2002 |>
ggplot() You should define a ggplot object with data layer gapminder_2022 and call this object p for further reuse.
Map variables gdpPercap and lifeExp to axes x and y. Define the axes. In ggplot2 parlance, this is called aesthetic mapping. Use aes().
Code
p <- p +
aes(x=gdpPercap, y=lifeExp)
p Use ggplot object p and add a global aesthetic mapping gdpPercap and lifeExp to axes x and y (using + from ggplot2) .
For each row, draw a point at coordinates defined by the mapping. You need to add a geom_ layer to your ggplot object, in this case geom_point() will do.
We add another layer to our graphical object.
Code
p <- p +
geom_point()
pWe are building a graphical object (a ggplot object) around a data frame (gapminder)
We supply aesthetic mappings (aes()) that can be either global or bound to some geometries (geom_point())or statistics
The global aesthetic mapping defines which columns are
- mapped to which axes,
- possibly mapped to colours, linetypes, shapes, …
Geometries and Statistics describe the building blocks of graphics
What’s missing here?
when comparing to the Gapminder demonstration, we can spot that
- colors are missing
- bubble sizes are all the same. They should reflect the population size of the country
- titles and legends are missing. This means the graphic object is useless.
We will add other layers to the graphical object to complete the plot
Second attempt: display more information
- Map
continentto color (useaes()) - Map
popto bubble size (useaes()) - Make point transparent by tuning
alpha(insidegeom_point()avoid overplotting)
Code
p <- p +
aes(color=continent, size=pop) +
geom_point(alpha=.5)
pIn this enrichment of the graphical object, guides have been automatically added for two aesthetics: color and size. Those two guides are deemed necessary since the reader has no way to guess the mapping from the five levels of continent to color (the color scale), and the reader needs help to connect population size and bubble size.
ggplot2 provides us with helpers to fine tune guides.
The scalings on the x and y axis do not deserve guides: the ticks along the coordinate axes provide enough information.
Scaling
In order to pay tribute to Hans Rosling, we need to take care of two scaling issues:
- the gdp per capita axis should be logarithmic
scale_x_log10() - the area of the point should be proportional to the population
scale_size_area()
Code
p <- p +
scale_x_log10() +
scale_size_area()
pMotivate the proposed scalings.
- Why is it important to use logarithmic scaling for gdp per capita?
- When is it important to use logarithmic scaling on some axis (in other contexts)?
- Why is it important to specify
scale_size_area()?
Code
p +
scale_radius()Scale for size is already present.
Adding another scale for size, which will replace the existing scale.
We use package patchwork to collect and present several graphical objects.
Code
ptchwrk <- (p + ggtitle("scale_size_area")) + (p + scale_size() + ggtitle("scale")) Scale for size is already present.
Adding another scale for size, which will replace the existing scale.
Code
ptchwrk + plot_annotation(
title='Comparing scale_size_area and scale_size',
caption='In the current setting, scale_size_area() should be favored'
)In perspective
- Add a plot title
- Make axes titles
- explicit
- readable
- Use
labs(...)
We should also fine tune the guides: replace pop by Population and titlecase continent.
Code
# TODO: fine tune the guides: replace `pop` by `Population` and titlecase `continent`.What should be the respective purposes of Title, Subtitle, Caption, … ?
Theming using ggthemes (or not)
Code
A theme defines the look and feel of plots
Within a single document, we should use only one theme
See Getting the theme for a gallery of available themes
Code
p +
theme_economist()Tuning scales
Use scale_color_manual(...) to fine tune the color aesthetic mapping.
Code
```{r}
#| label: theme_scale
neat_color_scale <-
c("Africa" = "#01d4e5",
"Americas" = "#7dea01" ,
"Asia" = "#fc5173",
"Europe" = "#fde803",
"Oceania" = "#536227")
```Code
p <- p +
scale_size_area(max_size = 15) + #<<
scale_color_manual(values = neat_color_scale) #<<Scale for size is already present.
Adding another scale for size, which will replace the existing scale.
Code
pChoosing a color scale is a difficult task
viridis is often a good pick.
Mimnimalist themes are often a good pick.
Code
old_theme <- theme_set(theme_minimal())Code
p <- p +
scale_size_area(max_size = 15,
labels= scales::label_number(scale=1/1e6,
suffix=" M")) +
scale_color_manual(values = neat_color_scale) +
labs(title= glue("Gapminder {min(gapminder$year)}-{max(gapminder$year)}"),
x = "Yearly Income per Capita",
y = "Life Expectancy",
caption="From sick and poor (bottom left) to healthy and rich (top right)") Scale for size is already present.
Adding another scale for size, which will replace the existing scale.
Scale for colour is already present.
Adding another scale for colour, which will replace the existing scale.
Code
p + theme(legend.position = "none") Zooming on a continent
Code
zoom_continent <- 'Europe' # choose another continent at your convenience Use facet_zoom() from package ggforce
Code
stopifnot(
require("ggforce") #<<
)
p_zoom_continent <- p +
facet_zoom( #<<
xy= continent==zoom_continent, #<<
zoom.data= continent==zoom_continent #<<
) #<<
p_zoom_continentAdding labels
Add labels to points. This can be done by aesthetic mapping. Use aes(label=..)
To avoid text cluttering, package ggrepel offers interesting tools.
Code
stopifnot(
require(ggrepel) #<<
)
p +
aes(label=country) + #<<
ggrepel::geom_label_repel(max.overlaps = 5) + #<<
scale_size_area(max_size = 15,
labels= scales::label_number(scale=1/1e6,
suffix=" M")) +
scale_color_manual(values = neat_color_scale) +
theme(legend.position = "none") +
labs(title= glue("Gapminder {min(gapminder$year)}-{max(gapminder$year)}"),
x = "Yearly Income per Capita",
y = "Life Expectancy",
caption="From sick and poor (bottom left) to healthy and rich (top right)")Facetting
So far we have only presented one year of data (2002)
Rosling used an animation to display the flow of time
If we have to deliver a printable report, we cannot rely on animation, but we can rely on facetting
Facets are collections of small plots constructed in the same way on subsets of the data
Add a layer to the graphical object using facet_wrap()
Code
p <- p +
aes(text=country) +
guides(color = guide_legend(title = "Continent",
override.aes = list(size = 5),
order = 1),
size = guide_legend(title = "Population",
order = 2)) +
theme(axis.text.x = element_text(angle = 45, vjust = 0.5, hjust=1)) +
facet_wrap(vars(year), ncol=6) +
ggtitle("Gapminder 1952-2007")
pAbide to the DRY principle using operator
%+%: theggplot2objectpcan be fed with another dataframe and all you need is proper facetting.
Code
p %+% gapminderAnimate for free with plotly
Use plotly::ggplotly() to create a Rosling like animation.
Use frame aesthetics.
Code
```{r}
#| label: animate
#| eval: !expr knitr::is_html_output()
#| code-annotations: hover
q <- filter(gapminder, FALSE) |>
ggplot() +
aes(x = gdpPercap) +
aes(y = lifeExp) +
aes(size = pop) +
aes(text = country) + #
aes(fill = continent) +
# aes(frame = year) + #
geom_point(alpha=.5, colour='black') +
scale_x_log10() +
scale_size_area(max_size = 15,
labels= scales::label_number(scale=1/1e6,
suffix=" M")) +
scale_fill_manual(values = neat_color_scale) +
theme(legend.position = "none") +
labs(title= glue("Gapminder {min(gapminder$year)}-{max(gapminder$year)}"),
x = "Yearly Income per Capita",
y = "Life Expectancy",
caption="From sick and poor (bottom left) to healthy and rich (top right)")
(q %+% gapminder) |>
plotly::ggplotly(height = 500, width=750)
```-
textwill be used while hovering -
frameis used byplotlyto drive the animation. Oneframeper year
Code
```{r}
#| eval: !expr knitr::is_html_output()
(p %+% gapminder +
facet_null() +
aes(frame=year)) |>
plotly::ggplotly(height = 500, width=750)
```